//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.OrderSet;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.GiftCard;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

@Repository
public class GiftCardDao extends BaseDao<GiftCard, String> {
    public GiftCardDao() {
    }

    public GiftCard getByCardCode(String cardCode) {
        return (GiftCard)this.getByWhere("cardCode=:p1", new Object[]{cardCode});
    }

    public String chkCardCodes(List<String> codes) {
        ArrayList params = new ArrayList();
        StringBuilder sql = new StringBuilder();
        sql.append("select top 1 cardCode from GiftCard where cardCode in (");

        for(int i = 0; i < codes.size(); ++i) {
            if(i > 0) {
                sql.append(", ");
            }

            sql.append(":p" + (i + 1));
            params.add(codes.get(i));
        }

        sql.append(")");
        return (String)this.getBySql(String.class, sql.toString(), params.toArray());
    }

    public boolean chkCardExists(String code1, String code2) {
        String sql = "select count(*) from GiftCard where cardCode>=:p1 and cardCode<=:p2";
        return ((Integer)this.getBySql(Integer.class, sql, new Object[]{code1, code2})).intValue() > 0;
    }

    public List<GiftCard> getNotReceiveList(String shopCode) {
        return this.queryList("shopCode=:p1 and status=1 and reportLoss=0", new Object[]{shopCode}, (new OrderSet()).asc("cardCode"), -1);
    }

    public List<GiftCard> getNotReceiveTypeCount(String shopCode) {
        String sql = "select cardType,count(*) status from GiftCard\nwhere shopCode=:p1 and status=1 and reportLoss=0 group by cardType order by cardType";
        return this.queryListBySql(GiftCard.class, sql, new Object[]{shopCode});
    }

    public void addCardList(List<String> cardList, String cardType, double cardAmount, String shopCode, String userCode) {
        String sql = "insert into GiftCard (CardCode, ShopCode, cardType, cardAmount, Status, CreateUser, CreateTime, UpdateTime) values (:p1, :p2, :p3, :p4, 1, :p5, getdate(), getdate())";
        SQLQuery query = this.getSession().createSQLQuery(sql);
        query.setString("p2", shopCode);
        query.setString("p3", cardType);
        query.setDouble("p4", cardAmount);
        query.setString("p5", userCode);
        Iterator i$ = cardList.iterator();

        while(i$.hasNext()) {
            String cardCode = (String)i$.next();
            query.setString("p1", cardCode);
            query.executeUpdate();
        }

    }

    public void addCardList(List<GiftCard> cardList, String userCode) {
        String sql = "insert into GiftCard (CardCode, ShopCode, cardType, cardAmount, Status, CreateUser, CreateTime, UpdateTime) values (:p1, :p2, :p3, :p4, 1, :p5, getdate(), getdate())";
        SQLQuery query = this.getSession().createSQLQuery(sql);
        Iterator i$ = cardList.iterator();

        while(i$.hasNext()) {
            GiftCard card = (GiftCard)i$.next();
            query.setString("p1", card.getCardCode());
            query.setString("p2", card.getShopCode());
            query.setString("p3", card.getCardType());
            query.setDouble("p4", card.getCardAmount().doubleValue());
            query.setString("p5", userCode);
            query.executeUpdate();
        }

    }

    public void delCardList(List<String> cardList) {
        String sql = "delete from GiftCard where CardCode = :p1 and status=1";
        SQLQuery query = this.getSession().createSQLQuery(sql);
        Iterator i$ = cardList.iterator();

        while(i$.hasNext()) {
            String cardCode = (String)i$.next();
            query.setString("p1", cardCode);
            query.executeUpdate();
        }

    }

    public void updateReceive(GiftCard card) {
        String sql = "update GiftCard set status=:p1, receiveUser=:p2, receiveDate=:p3 where cardCode=:p4";
        this.updateBySql(sql, new Object[]{card.getStatus(), card.getReceiveUser(), card.getReceiveDate(), card.getCardCode()});
    }

    public void updateLoss(GiftCard card) {
        String sql = "update GiftCard set reportLoss=:p1, lossUser=:p2, lossDate=:p3 where cardCode=:p4";
        this.updateBySql(sql, new Object[]{card.getReportLoss(), card.getLossUser(), card.getLossDate(), card.getCardCode()});
    }

    public void cancelLoss(String cardCode) {
        String sql = "update GiftCard set reportLoss=:p1 where cardCode=:p2";
        this.updateBySql(sql, new Object[]{Integer.valueOf(0), cardCode});
    }
}
